package com.zhouyiquan.qqserver.service;

import java.util.HashMap;
import java.util.Iterator;

/**
 * @Author: 周益全 (https://gitee.com/zfovikqdev928)
 * @BugFeedBackEmail: zfovikqdev928@126.com
 * @Date: Create in 2023-01
 * @Description: 该类用于管理和客户端通信的线程
 * @Version:
 */
public class ManageServerConnectClientThread {
    private static final HashMap<String, ServerConnectClientThread> hm = new HashMap<>();

    /**
     * 返回线程集合
     * @return
     */
    public static HashMap<String, ServerConnectClientThread> getHm() {
        return hm;
    }

    /**
     * 根据 userId 将线程加入集合中
     */
    public static void addServerConnectClientThread(String userId, ServerConnectClientThread serverConnectClientThread) {
        hm.put(userId, serverConnectClientThread);
    }


    /**
     * 从集合中移除某个线程对象
     */
    public static void removeThread(String userId) {
        if (userId != null) {
            hm.remove(userId);
        }
    }

    /**
     * 根据 userId 获取线程信息
     */
    public static ServerConnectClientThread getServerConnectClientThread(String userId) {
        return hm.get(userId);
    }

    /**
     * 返回所有的在线用户列表
     */
    public static String getOnlineUser() {
        // 迭代器遍历
        Iterator<String> iterator = hm.keySet().iterator();
        String onlineUserList = "";
        while (iterator.hasNext()) {
            onlineUserList += iterator.next().toString() + " ";
        }
        return onlineUserList;
    }


}

